import http from "@/api";
import AuthResponse from "@/models/AuthResponse";
import axios from "axios";
const proxyArray: Array<Array<string>> = JSON.parse(
  import.meta.env.VITE_PROXY || "[]"
);
console.log(proxyArray);

export default class LoginApi {
  static AUTHORIZE = proxyArray[0][1] + "/auth-api/oauth2/authorize";
  
  static CONSENT_PARAMS =
    proxyArray[0][1] + "/auth-api/oauth2/consent/parameters";
  static AUTH_HOST = proxyArray[0][1];
  static config = {
    // 默认地址
    baseURL: LoginApi.AUTH_HOST,
    // 跨域时候允许携带凭证
    withCredentials: true,
  };
  static service = axios.create(this.config);

  /**
   *
   * @param loginModel 用户登录表单数据
   * @returns
   */
  static loginApi = (loginModel: any) => {
    return http.postForm<any>("/auth-api/login", loginModel);
  };

  /**
   *
   * @param code 授权码
   * @returns 根据授权码获取token信息
   */
    static getAccessTokenByCode = (code: string) => {
      return http.postForm<AuthResponse>('/auth-api/oauth2/token', {
        client_secret: 'secret',
        client_id: 'client3',
        grant_type: 'authorization_code',
        redirect_uri: import.meta.env.VITE_REDIRECT_URI,
        code: code
      })
    }
  /**
   * 提交授权确认
   * @param data 客户端、scope等
   * @param requestUrl 请求地址(授权码与设备码授权提交不一样)
   * @returns 是否确认成功
   */
  static submitApproveScope(data: any, requestUrl: string) {
    const config = {
      // 默认地址
      baseURL: LoginApi.AUTH_HOST,
      // 跨域时候允许携带凭证
      withCredentials: true,
    };

    const instance = axios.create(config);
    instance.interceptors.response.use(
      (response) => {
        // Handle successful response
        return response;
      },
      (error) => {
        // Handle error response
        if (error.response && error.response.status == 302) {
          // It's a redirect
          const redirectUrl = error.response.headers.location;
          if (redirectUrl) {
            // Optionally, you could process the redirect URL as needed
            console.log("Redirect to:", redirectUrl);
            // Example: If you need to follow the redirect manually
            window.location.href = redirectUrl;
          }
        }
        return Promise.reject(error);
      }
    );
    return axios.create(config).post(requestUrl, data);
  }

  /**
   * 获取授权确认页面相关数据
   * @param queryString 查询参数，地址栏参数
   * @returns 授权确认页面相关数据
   */
  static getConsentParameters(queryString: string) {
    return axios
      .create(this.config)
      .get(`/auth-api/oauth2/consent/parameters${queryString}`, {});
  }
   /**
   * 获取授权确认页面相关数据
   * @param queryString 查询参数，地址栏参数
   * @returns 授权确认页面相关数据
   */
   static logout() {
    return axios
      .create(this.config)
      .get(`/auth-api/logout`, {});
  } 

  /**
   *
   * @param email 邮箱
   * @param type 用途
   * @returns 获取邮箱登录验证码
   */
  static sendEmailCode = (email: string) => {
    return http.get<string>('/auth-api/sms/sendEmail', {
      email: email,
      type: 'captcha'
    })
  }

  static getMenusByScopes=()=>{
    return http.get<Menu.MenuOptions[]>('/auth-api/menus/getMenusByScopes',{
      scopes:'list.add'
    })
  }

  
  
}
